/*
 * Copyright [2022] [https://www.xiaonuo.vip]
 *
 * Snowy采用APACHE LICENSE 2.0开源协议，您在使用过程中，需要注意以下几点：
 *
 * 1.请不要删除和修改根目录下的LICENSE文件。
 * 2.请不要删除和修改Snowy源码头部的版权声明。
 * 3.本项目代码可免费商业使用，商业使用请保留源码和相关描述文件的项目出处，作者声明等。
 * 4.分发源码时候，请注明软件出处 https://www.xiaonuo.vip
 * 5.不可二次分发开源参与同类竞品，如有想法可联系团队xiaonuobase@qq.com商议合作。
 * 6.若您的项目无法满足以上几点，需要更多功能代码，获取Snowy商业授权许可，请在官网购买授权，地址为 https://www.xiaonuo.vip
 */
package vip.xiaonuo.biz.modular.deviceinfo.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollStreamUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import vip.xiaonuo.biz.modular.devicetime.entity.DeviceTime;
import vip.xiaonuo.common.enums.CommonSortOrderEnum;
import vip.xiaonuo.common.exception.CommonException;
import vip.xiaonuo.common.page.CommonPageRequest;
import vip.xiaonuo.biz.modular.deviceinfo.entity.DeviceInfo;
import vip.xiaonuo.biz.modular.deviceinfo.mapper.DeviceInfoMapper;
import vip.xiaonuo.biz.modular.deviceinfo.param.*;
import vip.xiaonuo.biz.modular.deviceinfo.service.DeviceInfoService;

import java.util.List;

/**
 * 设备信息Service接口实现类
 *
 * @author Blurryface
 * @date  2025/05/14 21:33
 **/
@Service
public class DeviceInfoServiceImpl extends ServiceImpl<DeviceInfoMapper, DeviceInfo> implements DeviceInfoService {

    @Override
    public Page<DeviceInfo> page(DeviceInfoPageParam deviceInfoPageParam) {
        QueryWrapper<DeviceInfo> queryWrapper = new QueryWrapper<DeviceInfo>().checkSqlInjection();
        if(ObjectUtil.isNotEmpty(deviceInfoPageParam.getName())) {
            queryWrapper.lambda().like(DeviceInfo::getName, deviceInfoPageParam.getName());
        }
        if(ObjectUtil.isNotEmpty(deviceInfoPageParam.getOrgId())) {
            queryWrapper.lambda().eq(DeviceInfo::getOrgId, deviceInfoPageParam.getOrgId());
        }
        if(ObjectUtil.isNotEmpty(deviceInfoPageParam.getCode())) {
            queryWrapper.lambda().eq(DeviceInfo::getCode, deviceInfoPageParam.getCode());
        }
        if(ObjectUtil.isNotEmpty(deviceInfoPageParam.getTypeId())) {
            queryWrapper.lambda().eq(DeviceInfo::getTypeId, deviceInfoPageParam.getTypeId());
        }
        if(ObjectUtil.isNotEmpty(deviceInfoPageParam.getStatus())) {
            queryWrapper.lambda().eq(DeviceInfo::getStatus, deviceInfoPageParam.getStatus());
        }
        if(ObjectUtil.isNotEmpty(deviceInfoPageParam.getLocation())) {
            queryWrapper.lambda().eq(DeviceInfo::getLocation, deviceInfoPageParam.getLocation());
        }
        if(ObjectUtil.isAllNotEmpty(deviceInfoPageParam.getSortField(), deviceInfoPageParam.getSortOrder())) {
            CommonSortOrderEnum.validate(deviceInfoPageParam.getSortOrder());
            queryWrapper.orderBy(true, deviceInfoPageParam.getSortOrder().equals(CommonSortOrderEnum.ASC.getValue()),
                    StrUtil.toUnderlineCase(deviceInfoPageParam.getSortField()));
        } else {
            queryWrapper.lambda().orderByAsc(DeviceInfo::getSortCode);
        }
        return this.page(CommonPageRequest.defaultPage(), queryWrapper);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void add(DeviceInfoAddParam deviceInfoAddParam) {
        DeviceInfo deviceInfo = BeanUtil.toBean(deviceInfoAddParam, DeviceInfo.class);
        this.save(deviceInfo);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void edit(DeviceInfoEditParam deviceInfoEditParam) {
        DeviceInfo deviceInfo = this.queryEntity(deviceInfoEditParam.getId());
        BeanUtil.copyProperties(deviceInfoEditParam, deviceInfo);
        this.updateById(deviceInfo);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(List<DeviceInfoIdParam> deviceInfoIdParamList) {
        // 执行删除
        this.removeByIds(CollStreamUtil.toList(deviceInfoIdParamList, DeviceInfoIdParam::getId));
    }

    @Override
    public DeviceInfo detail(DeviceInfoIdParam deviceInfoIdParam) {
        return this.queryEntity(deviceInfoIdParam.getId());
    }

    @Override
    public DeviceInfo queryEntity(String id) {
        DeviceInfo deviceInfo = this.getById(id);
        if(ObjectUtil.isEmpty(deviceInfo)) {
            throw new CommonException("设备信息不存在，id值为：{}", id);
        }
        return deviceInfo;
    }

    @Override
    public List<DeviceInfo> listByType(String type) {
        QueryWrapper<DeviceInfo> queryWrapper = new QueryWrapper<DeviceInfo>();
        queryWrapper.select("ID", "NAME");
        if(ObjectUtil.isNotEmpty(type)) {
            queryWrapper.lambda().eq(DeviceInfo::getTypeId, type);
        }
        queryWrapper.eq("STATUS", "正常");
        queryWrapper.lambda().orderByAsc(DeviceInfo::getSortCode);
        return this.list(queryWrapper);
    }
}
